package com.hejia.bulbul.controller;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hejia.alauda.service.PaymentService;
import com.hejia.alauda.utils.HttpResponse;

/**
 * 银行卡控制器
 * @author hyr
 *
 */
@Controller
@RequestMapping("bankcards")
public class BankCardController {
	
	private static Log Logger = LogFactory.getLog(UserController.class);
	
	@Resource
	private PaymentService bankCardService;

	/**
	 * 更换银行卡
	 * @param originalBankCardId	原银行卡ID
	 * @param newBankCardId			新银行卡ID
	 * @return
	 */
	@RequestMapping(value = "change", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse changeBankCard(Long originalBankCardId, Long newBankCardId) {

		HttpResponse response = new HttpResponse();
		try {
			this.bankCardService.changeBankCard(originalBankCardId, newBankCardId);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("更换银行卡成功");
		} catch (Exception e) {
			Logger.error("更换银行卡失败", e);

			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("更换银行卡失败 " + e.getMessage());
		}

		return response;
	}
	
	/**
	 * 解绑连连支付
	 * 
	 * @return 解绑结果
	 */
	@RequestMapping(value = "unbindllpay", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse unbindLLPay(Long bankCardId) {

		HttpResponse response = new HttpResponse();
		try {
			this.bankCardService.unbindLLPay(bankCardId);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("解绑连连支付成功");
		} catch (Exception e) {
			Logger.error("解绑连连支付失败", e);

			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("解绑连连支付失败 " + e.getMessage());
		}

		return response;
	}
	
	/**
	 * 解绑银行卡
	 * 
	 * @return 解绑结果
	 */
	@RequestMapping(value = "unbindbankcard", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse unbindBankCard(Long bankCardId) {

		HttpResponse response = new HttpResponse();
		try {
			this.bankCardService.unbindBankCard(bankCardId);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("解绑银行卡成功");
		} catch (Exception e) {
			Logger.error("解绑银行卡失败", e);

			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("解绑银行卡失败 " + e.getMessage());
		}

		return response;
	}
}